import scrapy
from coin.items.items import CoinItem
from coin.items.price_item import PriceItem
import re
import json
import demjson
from pprint import pprint
import datetime
import string

import sys
sys.path.append("../..")
from dar.dar import Dar

class CoinSpider(scrapy.Spider):
    name = "coin"
    allowed_domains = ["coinmarketcap.com"]

    def __init__(self, category=None, *args, **kwargs):
        super(CoinSpider, self).__init__(*args, **kwargs)
        self.getStartUrls()

    def getStartUrls(self):
        self.currency_home = 'https://coinmarketcap.com/currencies/'
        self.coin_page = 'https://coinmarketcap.com/currencies/%s/'
        self.coin_list = 'https://files.coinmarketcap.com/generated/search/quick_search.json'
        coin_names = self.get_hot_coin().keys()
        self.start_urls = [self.coin_page % coin_name for coin_name in coin_names]
        #self.start_urls.insert(0, self.coin_list)

    def get_hot_coin(self):
        focus = Dar('user_coin_focus')
        focus_coins = focus.find(('distinct coin_symbol',), {'status': 1})
        hot = [hot.get('coin_symbol') for hot in focus_coins]
        coin = Dar('coin')
        top100 = coin.find(('symbol',), {'status':1, 'rank': '< 3', 'symbol': '<> "BTC"'})
        top100 = [top.get('symbol') for top in top100]
        symbols = ','.join(["'%s'" % symbol for symbol in hot + top100])
        on = 'in (%s)' % symbols
        on = on.encode('utf-8')
        hot_coins = Dar('coin').find(('symbol', 'name'), {'status':1, 'symbol': on})
        hot_coins = {coin.get('name') : coin.get('symbol') for coin in hot_coins}
        self.hot_coins = hot_coins
        return hot_coins

    def fresh_coin(self, response):
        coins = demjson.decode(response.body)
        coins = json.loads(json.dumps(coins))
        now = datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')
        for coin in coins:
            coin_item = CoinItem()
            coin_item['name'] = coin['name']
            coin_item['symbol'] = coin['symbol']
            coin_item['rank'] = coin['rank']
            coin_item['slug'] = coin['slug']
            coin_item['updated_at'] = now
            yield coin_item

    def save_price(self, response):
        coin_name = response.url[len(self.currency_home): -1]
        anchor = 'div > span#quote_price'
        # usdt
        usdt_price = response.css(anchor + ' > span::text').extract()[0]
        if isinstance(usdt_price, str):
            usdt_price = string.replace(usdt_price, '%', '', 10)
            usdt_price = string.replace(usdt_price, ' ', '', 10)
        anchor += ' + span'
        usdt_ratio = response.css(anchor + '::text').extract()[0][1:-1]
        usdt_ratio = string.replace(usdt_ratio, '%', '', 10)
        usdt_ratio = string.replace(usdt_ratio, ' ', '', 10)
        # btc
        anchor += ' + br + span'
        btc_price = response.css(anchor + '::text').extract()
        if btc_price:
            btc_price = btc_price[0]
            btc_price = string.replace(btc_price, 'BTC', '', 10)
            btc_price = string.replace(btc_price, ' ', '', 10)
        else:
            btc_price = 0
        anchor += ' + span'
        btc_ratio = response.css(anchor + '::text').extract()
        if btc_ratio:
            btc_ratio = btc_ratio[0].encode('utf-8')[2:-1]
        else:
            btc_ratio = 0
        if isinstance(btc_ratio, str):
            btc_ratio = string.replace(btc_ratio, '%', '', 10)
            btc_ratio = string.replace(btc_ratio, ' ', '', 10)
        # eth
        anchor += ' + br + span'
        eth_price = response.css(anchor + '::text').extract()
        if eth_price:
            eth_price = eth_price[0]
        else:
            eth_price = 0
        if isinstance(eth_price, str):
            eth_price = string.replace(eth_price, 'BTC', '', 10)
            eth_price = string.replace(eth_price, ' ', '', 10)
        anchor = anchor + ' + span'
        eth_ratio = response.css(anchor + '::text').extract()
        if eth_ratio:
            eth_ratio = eth_ratio[0][1:-1]
        else:
            eth_ratio = 0
        if isinstance(eth_ratio, str):
            eth_ratio = string.replace(eth_ratio, '%', '', 10)
            eth_ratio = string.replace(eth_ratio, ' ', '', 10)
        price = {
            'coin_symbol': self.hot_coins[coin_name],
            'usdt_price': usdt_price,
            'usdt_ratio': usdt_ratio,
            'btc_price': btc_price,
            'btc_ratio': btc_ratio,
            'eth_price': btc_price,
            'eth_ratio': btc_ratio,
        }
        priceItem = PriceItem(price)
        yield priceItem

    def parse(self, response):
        if response.url == self.coin_list:
            yield scrapy.Request(response.url, callback=self.fresh_coin)
        elif response.url[0: len(self.currency_home)] == self.currency_home:
            yield scrapy.Request(response.url, callback=self.save_price)
        else:
            yield None

        #priceSelector = 'span#quote_price'
        #price = response.css(priceSelector).extract()
        #print price
        #catesSelector = 'a[clstag*="mbNav"]::text'
        #runTypeSelector = 'a[clstag*="dianpuname1"] + em.u-jd span::text'
        #params = response.xpath('//head/script/text()').extract()[0]
        #navs = response.css(catesSelector).extract()
        #runType = response.css(runTypeSelector).extract()
        #conf = re.findall(r'var.*?=\s*(.*?);', params, re.DOTALL | re.MULTILINE)
        #conf = demjson.decode(conf[0])
        #conf = json.loads(json.dumps(conf))['product']
        #shopId = conf['shopId']
        #venderId = conf['venderId']
        #cateIds = conf['cat']
        #skuId = conf['skuid']
        #goods = JdGoodsItem()
        #if runType:
        #    goods['selfRun'] = 1
        #else:
        #    goods['selfRun'] = 0
        #goods['skuId'] = skuId
        #goods['cate1'], goods['cate2'], goods['cate3'] = cateIds[0 : 3]
        #goods['cate1Name'], goods['cate2Name'], goods['cate3Name'] = navs[0 : 3]
        #goods['venderId'] = venderId
        #goods['shopId'] = shopId
        #yield goods
